function [GG,RR,eu,SDX,flag,ACont,ALag,ALead,PSI] =mod_flexprice_SSrules(param,mspec)
 
    %MSPEC 1  Classical Monetary Model, 2 NK Model
    initss  = [];     ssvec   = [];
%     
%     flag.ssok   = 1;    % Steady-State OK (Not Changed in this Model)
%     flag.solver = 1;    % Which Solver to Use (Not Change in this model)
%     
    % =========================================================================
    %%  PART I: Parameters and Positions
    bet         = param(1);
    alpha       = param(2);
    N           = param(3);  
    AD          = param(4); 
    PI          = param(5);
    b           = param(6);
    kap         = param(7); %slope of the NKPC. Matters only if mspec=2;
    phipiM      = param(8);
    deltabM     = param(9);
    phipiF      = param(10);
    deltabF     = param(11);
    phiy        = param(12);
    rhomp       = param(13);
    rhotau      = param(14);
    rhoa        = param(15);
    STDmp       = param(16);
    STDtau      = param(17);
    STDa        = param(18);
    %

    rho=(1-1/AD)/bet;
    
    
    %%  PART II: Position of Variables
    % $ X_t = [c_t L_t u_t k_t i_t x^h_t h_t m_t \lambda^1_t \lambda^4_t w^h_t r^k_t mc_t \pi^w_t w_t \mu^w_t ...
    %           \pi_t y_t R_t \lambda^2_t k^s_t \pi_{t-1} y_{t-1} y^{obs}_t c^{obs}_t i^{obs}_t w^{obs}_t ...
    %           z_t \varepsilon^a_t \varepsilon^b_t \varepsilon^{\pi^{drift}}_t \varepsilon^g_t \varepsilon^h_t ...
    %           \varepsilon^i_t \varepsilon^l_t \varepsilon^{\lambda^p}_t \theta^{\lambda^p}_t \varepsilon^{\lambda^w}_t \theta^{\lambda^w}_t ...
    %           \varepsilon^{\nu}_t \varepsilon^{\omega}_t \varepsilon^{f^a}_t \varepsilon^{f^b}_t \pi^d_t \pi^g_t \pi^d_{t-1} \pi^g_{t-1} ...
    %           E_t \pi_{t+i} E_t R_{t+i} ]
    %  Split by section, numbered more variables relatively for greater ease of
    %  modification. Added  - Theodore 8/10/2015
    %
    YLog=1;
    NLog=2;
    WrLog=3;
    rLog=4;
    RLog=5;
    piLog=6;
    pbLog=7;
    bLog=8; %Debt to output ratio
      % AR exogenous processes
    tauLog=9;
    %Counterfactual economy
    YcLog=10;
    NcLog=11;
    WrcLog=12;
    rcLog=13;
    RcLog=14;
    picLog=15;
    pbcLog=16;
    bcLog=17;
    taucLog=18;
    %Shocks
    shkmpLog     = 19;      %1 monetary policy shock
    shktauFLog   =20;      % goverment policy shock to surplus Funded
    shktauULog   =21;      % goverment policy shock to surplus Unfunded
    shkaLog=22;
    
         
    % Final Counts
    NVarY   = shkaLog;
    Neq     = shkaLog;
    
    %%  PART III: Position of shocks
    % 3.2 EPSILON Variable Assignment
    % Excludes measurement errors for Shell,
    % Inflation Exp and Relative price, except for
    % the Covariance matrix of the pD and pG VAR
    %
    % Z_t = [\eta^{a}_t \eta^{b}_t \eta^{\pi^{drift}}_t \eta^{g}_t \eta^{g^{drift}}_t \eta^{h}_t ...
    %        \eta^{i}_t \eta^{l}_t \eta^{\lambda^p}_t \eta^{\lambda^w}_t \eta^{\nu}_t \eta^{\omega}_t ...
    %        \eta^{f^a}_t \eta^{f^b}_t \eta^{\pi^d}_t \eta^{\pi^g}_t \eta^{r}_t \eta^{E_tr_{t+i}}_t]
    

        emp    = 1;
        etauF  = 2;
        etauU  = 3;
        ea     = 4;
        NShocks= 4;

        % Matrix of Standard Deviations
        SDX=eye(NShocks);
        SDX(emp,emp)              = STDmp;
        SDX(etauF,etauF)            = STDtau;
        SDX(etauU,etauU)            = STDtau;
        SDX(ea,ea)                = STDa;

    
 

    
    %%  PART IV: Model Declaration
    ACont   = zeros(Neq,NVarY);
    ALead   = zeros(Neq,NVarY);
    ALag    = zeros(Neq,NVarY);
    PSI     = zeros(Neq,NShocks);
    flag=1;
    
    %%  PART V: Steady-State
    % 5.0 Preliminaries
    %%  PART V: Steady-State
    % 5.0 Preliminaries
    
r=1/bet;
R=r*PI;
Y=N^(1-alpha);
Wr=(1-alpha)*N^(-alpha);
%phi=(log(Wr)-log(Y))/log(N);
phi=1/Y*Wr*(1-N); % L: log disutility of labor. 
Pb=1/(R-rho);  %L: allow steady inflation to be different from zero (PI=1). But we set it to be zero 
tau=((1+rho*Pb)/Pb-1)*b;

    %%  PART VI: Equations
    %NOTE variables indexed at time t and t+1 are written on the LHS of the
    %equation and lagged variables to the right
    ii=1;
    
    %% 1) Euler equation
     ACont(ii,YLog)       = -1;
     if mspec==1
         ACont(ii,rLog)       = -1;
    elseif mspec==2
         ACont(ii,RLog)       = -1;
         ALead(ii,piLog)      =  1; 
     end
     ALead(ii,YLog)       =  1;
    ii=ii+1;
    
    %% 2) Labor supply
    %% Leo: Not sure I understand how you derived this
    LaborSupplyEq; 
    ii=ii+1;
   
    if mspec==1 %Classical monetary model
        %% 3) Labor demand
        ACont(ii,WrLog)       = 1;
        ACont(ii,NLog)        = -alpha; %-(alpha-1);
        ACont(ii,shkaLog)     = -1;
        ii=ii+1;    
    elseif mspec==2 %NK model
        %% 3) NK Phillips curve
        ACont(ii,piLog)=1;
        %ACont(ii,YLog)=-kap;
        ACont(ii,WrLog)=-kap; % Renato replaced the eq. above with this one 16/9/2022
        ACont(ii,shkaLog)=kap;
        ALead(ii,piLog)=-bet;
        ii=ii+1;
    end
    
    %% 4) Production function
    ACont(ii,YLog)       = 1;
    ACont(ii,NLog)       = -(1-alpha); %-alpha;
    ACont(ii,shkaLog)    = -1;
    ii=ii+1;
    
    %% 5) Real rate definition
    ACont(ii,RLog)       = 1;
    ACont(ii,rLog)       = -1;   
    ALead(ii,piLog)      = -1;
    ii=ii+1;
    
    %% 6 ) Evolution of Debt
    DebtEq;
    ii=ii+1;
    
    %% 7 ) Price of Long term debt Debt
    PbEq;
    ii=ii+1;
    
    %% 8) Fiscal Rule
    ACont(ii,tauLog)=1;
    ACont(ii,shktauFLog)=-1;
    ACont(ii,shktauULog)=-1;
    
    ALag(ii,bcLog)=deltabM-deltabF;
    ALag(ii,bLog)=deltabF;
    ii=ii+1;
    
    
    %% 9) Taylor rule
    ACont(ii,RLog)       = 1;
    ACont(ii,picLog)     = -phipiM+phipiF;
    ACont(ii,piLog)      =  -phipiF;
    ACont(ii,YLog)       =phiy;
    ACont(ii, shkmpLog)  = -1;
    ii=ii+1;
    
    %% COUNTERFACTUAL ECONOMY
    
     %% 10) Euler equation
     ACont(ii,YcLog)       = -1;
     if mspec==1
         ACont(ii,rcLog)       = -1;
     elseif mspec==2
         ACont(ii,RcLog)       = -1;
         ALead(ii,picLog)      =  1;
     end
     ALead(ii,YcLog)       =  1;
    ii=ii+1;
    %% 11) Labor supply
    LaborSupplyEq_c; 
    ii=ii+1;
    
    if mspec==1 %Classical monetary model
        %% 12) Labor demand
        ACont(ii,WrcLog)       = 1;
        ACont(ii,NcLog)       = -alpha; %-(alpha-1);
        ACont(ii,shkaLog)     = -1;
        ii=ii+1;    
    elseif mspec==2 %NK model
        %% 12) NK Phillips curve
        ACont(ii,picLog)=1;
        %ACont(ii,YcLog)=-kap;
        ACont(ii,WrcLog)=-kap; % Renato replaced the eq. above with this one 16/9/2022
        ACont(ii,shkaLog)=kap;
        ALead(ii,picLog)=-bet;
        ii=ii+1;
    end
    
    %% 13) Production function
    ACont(ii,YcLog)       = 1;
    ACont(ii,NcLog)       = -(1-alpha); %-alpha;
    ACont(ii,shkaLog)     = -1;
    ii=ii+1;
    
    %% 14) Real rate definition
    ACont(ii,RcLog)       = 1;
    ACont(ii,rcLog)       = -1;   
    ALead(ii,picLog)      = -1;
    ii=ii+1;
    
    %% 15) Evolution of Debt
    DebtEq_c;
    ii=ii+1;
    
    %% 16 ) Price of Long term debt Debt
    PbEq_c;
    ii=ii+1;
    
     %% 17) Fiscal Rule
    ACont(ii,taucLog)=1;
    ACont(ii,shktauFLog)=-1;
%     ACont(ii,shktauULog)=-1;
    
    ALag(ii,bcLog)=deltabM;    
    ii=ii+1;
    
     %% 18) Taylor rule
    ACont(ii,RcLog)       = 1;
    ACont(ii,picLog)      = -phipiM;
    ACont(ii,YcLog)       = -phiy;
    ACont(ii, shkmpLog)   = -1;
    ii=ii+1;
       
    
    %% Exogenous Processes
    
    %% 19) Monetary shock
    ACont(ii,shkmpLog)  = 1;
    PSI(ii,emp)      = 1;
    ALag(ii,shkmpLog)   = rhomp;
    ii=ii+1;
    
    %% 20) Fiscal shock (Funded)
    ACont(ii,shktauFLog)  = 1;
    PSI(ii,etauF)      = 1;
    ALag(ii,shktauFLog)   = rhotau;
    ii=ii+1;
    
     %% 21) Fiscal shock (Ununded)
    ACont(ii,shktauULog)  = 1;
    PSI(ii,etauU)      = 1;
    ALag(ii,shktauULog)   = rhotau;
    ii=ii+1;
    
    %% 22) Technology shock
    ACont(ii,shkaLog)  = 1;
    PSI(ii,ea)      = 1;
    ALag(ii,shkaLog)   = rhoa;
    %ii=ii+1;
    
    
    %%  PART VII: Solve Model
    % AMA Solver
    [GG,~,RR,~,~,~,~,eu]=amasolve(ALead,ACont,ALag,zeros(NVarY,1),PSI);
    
%     %Adding The EFFR
%     for i=1:nfwg
%        GGG=GG^(i+1); RRR=GG^i*RR;
%        GG(end+1,:)=GGG(rLog,:); GG(1:end,end+1)=zeros(size(GG,1),1);
%        RR(end+1,:)=RRR(rLog,:);
%     end
    %     eu
    if isequal(eu(:),[1;1])==false
        
        % Clean
        ZZ  = [];
       % Cobs= [];
        SDX = [];
        RR  = [];
        ssvec=[];
        ssnames={};
        namesstates={};
        namesshocks={};
        disp('Model has no solution')
        flag=0;
        %         return
    else
        %% Measurement Equations
        
        % Data Numbers
        %         v_data          = 1;
        %        jfr_data          = 1;
        %urate_data          = 1;
        %ee_data         = 2;
%         if ~isempty(EFFR)
%             ffr_data      =3;
%             effr_data=4:3+nfwg/3;
%             
%             NObs=3+nfwg/3;
%         else
         %   NObs=2;
%         end
        %inizialize Measurement Matrices
        %ZZ=zeros(NObs,size(GG,1));
        %Cobs=zeros(NObs,1);
        
        %Vacancy rate
        %         ZZ(v_data,thetaLog)=U0star+s*(1-U0star);
        %         ZZ(v_data,uzeroLog)=(1-s)*U0star;
        %         Cobs(v_data)=phi0;
        
        %Job finding rate
        %ZZ(jfr_data,phiLog)=1;
        %Cobs(jfr_data)=phi0; % was log(phi0), Modified by Renato 21/12
        
        % Unemploment Rate
        %         ZZ(urate_data, uzeroLog)=1;
%         ZZ(urate_data,shkdeltaLog) = Nstar;  %shock to the Separation rate
%         
%         ZZ(urate_data,uLog)        = 1;
%         ZZ(urate_data,nLog)        = delta;
%         Cobs(urate_data)=U0star;
        
        %E2E Flow Rate
        %         ZZ(ee_data, eeLog)=.84;
        %ZZ(ee_data, eeLog)=1; %ZZ(ee_data,shksTrendLog)=1;
        %Cobs(ee_data,1)=EEstar;
%         if ~isempty(EFFR)
%             %Current FFR
%             ZZ(ffr_data, rLog)=1;
%             Cobs(ffr_data,1)=log(Rstar);
%             
%             Expected FFR
%             ZZ(effr_data,EFFR)=kron(eye(length(effr_data)),ones(1,3))/3;
%             Cobs(effr_data,1)=log(Rstar)*ones(nfwg/3,1);
%         end
        %ssvec=[];
        %ssnames={};
        %namesstates={};
        %namesshocks={};
    %end
end


